using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using System;
using System.IO;
public class Unity2CocosSceneExportWindow : EditorWindow
{

    [MenuItem("Window/Unity2CocosScene")]  //���Ӳ˵�ѡ��
    public static void ShowWindow()
    {
        var window = EditorWindow.GetWindow<Unity2CocosSceneExportWindow>();
        window.Show();
        window.ExportPath = EditorPrefs.GetString("ExportPath");
        if (string.IsNullOrEmpty(window.ExportPath))
        {
            window.ExportPath = Application.dataPath;
            EditorPrefs.SetString("ExportPath", window.ExportPath);
        }
    }
    private string ExportPath = "";
    private bool IgnoreNoActive = false;
    private bool IgnoreNoEnable = false;
    private bool IsOverride = false;
    private void OnGUI()
    {
        if (GUILayout.Button("选择导出路径"))
        {
            this.ExportPath = EditorUtility.OpenFolderPanel("导出路径", this.ExportPath, this.ExportPath);
            EditorPrefs.SetString("ExportPath", this.ExportPath);
        }
        EditorGUILayout.LabelField("导出路径", this.ExportPath);
        this.IgnoreNoActive = EditorGUILayout.Toggle("是否忽略隐藏物体", this.IgnoreNoActive);
        this.IgnoreNoEnable = EditorGUILayout.Toggle("是否忽略禁用组件", this.IgnoreNoEnable);
        this.IsOverride = EditorGUILayout.Toggle("是否覆盖原来文件", this.IsOverride);
        bool isConfirm = true;
        if (GUILayout.Button("导出当前场景") && isConfirm)
        {
            if (this.IsOverride)
            {
                isConfirm = UnityEditor.EditorUtility.DisplayDialog("警告", "是否覆盖原来文件,覆盖后将无法恢复", "确认", "取消");
            }
            if (isConfirm)
            {
                AssetHelper.Init();
                JToken cocosObjectJObject = JsonConvert.DeserializeObject<JObject>(Resources.Load<TextAsset>("CocosObjectConfig").text);
                JToken cocosMetaJObject = JsonConvert.DeserializeObject<JObject>(Resources.Load<TextAsset>("CocosMetaConfig").text);
                JToken cocosNodeJObject = JsonConvert.DeserializeObject<JObject>(Resources.Load<TextAsset>("CocosNodeConfig").text);
                JToken cocosShaderJObject = JsonConvert.DeserializeObject<JObject>(Resources.Load<TextAsset>("CocosShaderConfig").text);

                JArray sceneArrary = new JArray();
                var scene = EditorSceneManager.GetActiveScene();
                var exportsetting = new ExportSetting(Guid.NewGuid().ToString(), this.IgnoreNoActive, this.IgnoreNoEnable, this.IsOverride, this.ExportPath, cocosObjectJObject, cocosMetaJObject, cocosNodeJObject, cocosShaderJObject);
                AssetHelper.DirectoryCreate(exportsetting.ExportPath + "/Mesh");
                AssetHelper.DirectoryCreate(exportsetting.ExportPath + "/Texture");
                AssetHelper.DirectoryCreate(exportsetting.ExportPath + "/Material");
                AssetHelper.DirectoryCreate(exportsetting.ExportPath + "/Prefab");
                ExportToDispatcher.Instance.ParseObjectToCocos(scene, exportsetting, sceneArrary);
            }

        }
        if (GUILayout.Button("导出选中预设体") && isConfirm)
        {
            if (this.IsOverride)
            {
                isConfirm = UnityEditor.EditorUtility.DisplayDialog("警告", "是否覆盖原来文件,覆盖后将无法恢复", "确认", "取消");
            }
            if (isConfirm)
            {

            }
        }
    }
    private void CreateCocosScene()
    {
        JArray jArray = new JArray();
    }
}

